import { defineStore } from 'pinia'
import { accurateToFixed, fillTimeSeriesData } from '@/utils/index'

export interface PreviewData {
  date: string
  vn: number
  cn: number
  pn: number
  pnRate: string
  cnRate: string
}
interface Params {
  startData: PreviewData[]
  endData: PreviewData[]
}

export const useExposureLineStore = defineStore('ExposureLine', {
  state: () => {
    return {
      loading: false,
      startData: [] as PreviewData[],
      endData: [] as PreviewData[],
      intervalType: '1',
    }
  },
  actions: {
    setLoading(loading: boolean) {
      this.loading = loading
    },
    setPreviewData({ startData, endData }: Params) {
      this.startData = fillTimeSeriesData(
        startData.map(elem => {
          return {
            ...elem,
            pnRate: accurateToFixed(Math.round((elem.pn / elem.cn) * 100000) / 100, 2),
            cnRate: accurateToFixed(Math.round((elem.cn / elem.vn) * 10000) / 100, 2),
          }
        }),
        +this.intervalType,
      )
      this.endData = fillTimeSeriesData(
        endData.map(elem => {
          return {
            ...elem,
            pnRate: accurateToFixed(Math.round((elem.pn / elem.cn) * 100000) / 100, 2),
            cnRate: accurateToFixed(Math.round((elem.cn / elem.vn) * 10000) / 100, 2),
          }
        }),
        +this.intervalType,
      )
    },
    setIntervalType(intervalType: string) {
      this.intervalType = intervalType
    },
  },
})
